package Java对象比较;


import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;


//class Student implements Comparable<Student>{
//    private String name;
//    private int age;
//
//    public Student(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//
//    @Override
//    public int compareTo(Student o) {
//        return this.age-o.age;
//    }
//}
class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

//自定义比较器
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        //Student o1是插入的元素
        return o1.getAge()-o2.getAge();//小堆
    }
}

class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());//name 是个字符串，字符串有自己的 compareTo 方法进行比较
    }
}
public class TestDemo1 {
    //找到数组中前k个最小数据——建大堆
    public static void topK(int[] arr,int k) {
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(k,new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//观察源码可知 ：compare 的结果大于 0 ，不需要进行调整，大堆即之前的o2大于现在的o1,则不调整
            }
        });//创建大堆

        for (int i = 0; i < arr.length; i++) {
            if (priorityQueue.size()<k) {
                priorityQueue.offer(arr[i]);
            }else {
                //前k个元素都已进入 大小为k的堆，可以开始往后调整
                if (priorityQueue.peek()>arr[i]) {
                    priorityQueue.poll();//出堆顶元素
                    priorityQueue.offer(arr[i]);//当前元素入堆
                }
            }
        }
        System.out.println(priorityQueue);

    }
    public static void main3(String[] args) {
        int[] array={1,31,2,55,6,7,8,9};

        topK(array,3);
    }




    public static void main(String[] args) {
        Student[] suts=new Student[2];
        suts[0]=new Student("ss",20);
        suts[1]=new Student("jj",18);

        Arrays.sort(suts,new NameComparator());//传入年龄比较器
        System.out.println(Arrays.toString(suts));
//        Arrays.sort(suts);
//        System.out.println(Arrays.toString(suts));


    }

    public static void main1(String[] args) {
        PriorityQueue<Student> priorityQueue1=new PriorityQueue<>();//无参，默认用的是 Comparable

        //PriorityQueue<Student> priorityQueue=new PriorityQueue<>(new AgeComparator());//传入一个年龄比较器
//        priorityQueue.offer(new Student("ss",20));
//        priorityQueue.offer(new Student("jj",18));
//        System.out.println(priorityQueue);

    }
}
